import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib import rcParams

# from sklearn.metrics import normalized_mutual_info_score

config = {
    "font.family": 'serif',
    "font.size": 12,
    "mathtext.fontset": 'stix',
    "font.serif": ['Times New Roman'],
}
rcParams.update(config)
# plt.rc('font', family='Times New Roman')  # 设置输出字体为新罗马family='Times New Roman'
# plt.rc('font', family='Arial')  # 设置输出字体为新罗马family='Arial'

df = pd.read_csv('nuclear_relative_condition.csv')
# df.columns = ['co_alpha', 'co_k', 'co_edges', 'wildbird_alpha', 'wildbird_k', 'wildbird_edges', 'ant_alpha', 'ant_k', 'ant_edges',
#                 'nuclear_alpha', 'nuclear_k', 'nuclear_edges'
#               ]
df.columns = [
                'alpha', 'k', 'edges' # nuclear
              ]
#
# df.columns = [
#                 'alpha_out','k_out','out_edges',
#                 'alpha_in', 'k_in', 'in_edges'
#               ]

y2 = df['alpha']
y1 = df['k']
x = df['edges']


# =====================================================绘制相对条件数与alpha========================================================
# 找到最小值点，这里使用np.argmax函数查找y数组中的最大值的索引
# min_idx = np.argmin(y1)
# min_idx = 7         # 挖掘机
# min_idx = 10         # nuclear
# min_x, min_y = x[min_idx], y1[min_idx]
# min_xa, min_ya = x[min_idx], y2[min_idx]
# fig, ax1 = plt.subplots()
# # plt.xticks(rotation=45)
#
# plt.grid(linestyle=':')  # 显示网格
# ax1.plot(x, y1, marker='o', linestyle='-', lw=2, markerfacecolor='white', color="#00579C", markersize='4', label="$k$")
# ax1.tick_params(axis='y', labelsize=13)
# ax1.tick_params(axis='x', labelsize=13)
# ax1.set_xlabel("Edges No.", fontsize='15')
# ax1.set_ylabel("Relative Condition Number $(k)$", fontsize='15')
#
# ax2 = ax1.twinx()
# ax2.plot(x, y2, marker='o', linestyle='-', lw=2, markerfacecolor='white', color="black", markersize='4',
#          label="$α_{ij}$")
# ax2.set_ylabel("Statistical Significance Probabilities $(α_{ij})$", fontsize='15')
# ax2.tick_params(axis='y', labelsize=12)
#
# ax1.scatter(min_x, min_y, color='r', s=20, zorder=3)  # 在最小值点上绘制一个红色的圆点
# ax2.scatter(min_xa, min_ya, color='r', s=20, zorder=3)  # 在最小值点上绘制一个红色的圆点
#
# # nuclear坐标
# ax2.annotate(f"$α_{{ij}} ={min_ya:.2f}$", xy=(min_xa, min_ya), xytext=(min_xa + 8, min_ya),      # {{ij}}双括号是为了将{}转义为latex格式的括号
#              arrowprops=dict(facecolor='black', shrink=0.05))  # 添加注释，使用红色箭头连接注释和最小值点
# ax1.annotate(f'$k(L_G,L_P)={min_y:.4f}$', xy=(min_x, min_y), xytext=(min_x + 8, min_y + 10),
#              arrowprops=dict(facecolor='#00579C', shrink=0.05))  # 添加注释，使用红色箭头连接注释和最小值点
#
# # wajueji 坐标
# # ax2.annotate(f"$α_{{ij}} ={min_ya:.2f}$", xy=(min_xa, min_ya), xytext=(min_xa + 800, min_ya),      # {{ij}}双括号是为了将{}转义为latex格式的括号
# #               arrowprops=dict(facecolor='black', shrink=0.05))  # 添加注释，使用红色箭头连接注释和最小值点
# # ax1.annotate(f'$k(L_G,L_P)={min_y:.4f}$', xy=(min_x, min_y), xytext=(min_x, min_y - 4),
# #               arrowprops=dict(facecolor='#00579C', shrink=0.05))  # 添加注释，使用红色箭头连接注释和最小值点
#
# fig.legend(loc="upper right", bbox_to_anchor=(1 - 0.1, 1), bbox_transform=ax1.transAxes)
# plt.rcParams['savefig.dpi'] = 600
# plt.show()

# 聚类结果1
# # labels_pred = [1, 1, 1, 1, 2, 1, 2, 2, 4, 1, 4, 4, 1, 1, 5, 3, 5, 5, 3, 3, 5, 4, 4, 1, 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 1, 1, 5, 5, 1, 5, 1, 1, 2,
# #                2, 4, 4, 1, 4, 4, 4, 4, 5, 2, 5, 1, 1, 1, 2, 1, 1, 2, 4, 4, 3, 2, 2, 2, 3, 3, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 4, 4, 2]

# 绘制NMI
data = pd.read_csv('挖掘机每次Louvain算法迭代结果（用于计算nmi）.csv')
data.columns = ['iteration1', 'iteration0', 'result']
#
# # 聚类结果2（可能由不同的聚类算法得到）
labels_true = data['result']
# labels_pred = data['iteration0']
# # 计算NMI
# nmi = normalized_mutual_info_score(labels_true, labels_pred)
# print(f"NMI: {nmi}")
init_NMI = [0.99, 0.944]
backbone_NMI = [1.]
iter_init = [1, 2]
iter_backbone = [1]
fig, ax1 = plt.subplots()
# plt.xticks(rotation=45)
x_ticks = [0, 1, 2]  # 只想显示0, 1, 2这三个刻度
plt.xticks(x_ticks)
plt.grid(linestyle=':')  # 显示网格
ax1.plot(iter_init, init_NMI, marker='*', linestyle='-', lw=2, color="#00579C", markersize='8', label="Partition of the original graph")
ax1.tick_params(axis='y', labelsize=15)
ax1.tick_params(axis='x', labelsize=15)
ax1.set_xlabel("Iterations", fontsize='18')

ax1.scatter(iter_backbone, backbone_NMI, color='black', s=25, zorder=3, label="Partition of the sparse graph")
# ax1.scatter(iter_backbone, backbone_NMI, marker='o', color="black", s='6',
#          label="Partition of sparse graph")
ax1.set_ylabel("NMI", fontsize='18')
ax1.tick_params(axis='y', labelsize=15)



fig.legend(loc="upper right", bbox_to_anchor=(1, 1), bbox_transform=ax1.transAxes)
plt.rcParams['savefig.dpi'] = 600
plt.show()
